package com.culture.bootdemo.utils.fileutils;

import com.alibaba.excel.annotation.ExcelProperty;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * @Author: wen
 * @DateTime: 2024/2/21 17:03
 * @Description:
 **/
public class EasyExcelFiledUtil {

    /**
     * 可以动态赋值表头内容
     *
     * @param t
     * @param keys
     * @param <T>
     * @return
     */
    public static <T> Class<T> getClassNew(T t, Map<String, String> keys) {
        if (t == null) {
            return null;
        }
        try {
            for (String key : keys.keySet()) {
                Field value = t.getClass().getDeclaredField(key);
                value.setAccessible(true);
                ExcelProperty property = value.getAnnotation(ExcelProperty.class);
                InvocationHandler invocationHandler = Proxy.getInvocationHandler(property);
                Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
                memberValues.setAccessible(true);
                Map<String, Object> values = (Map<String, Object>) memberValues.get(invocationHandler);
                values.put("value", new String[]{keys.get(key)});
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (Class<T>) t.getClass();
    }
}
